import XDate from '@/common/js/xdate.min.js'      //日期插件

/**  
 * @description 判断是否为空
 * @param {Object} value
 * @return {Boolean}  
 */ 
const isEmpty = function(value) {
    if (value == null || value == "" || typeof(value) == undefined) {
        return true;
    } else {
        return false;
    }
}

/**  
 * @description 替换数据
 * @param {String} sourceText 原文本
 * @param {String} oldText    被替换文本
 * @param {String} newText    新文本
 * @return {Boolean}  
 */ 
const replaceAll = function (sourceText, oldText, newText) {
	var reg=new RegExp(oldText,"g"); //创建正则RegExp对象   
	return sourceText.replace(reg,newText); 
}

/**  
 * @description 手机格式化
 * @param {String} value
 * @return {String}  
 */ 
const mobileFmt = (value)  => {
	if (isEmpty(value)) {
		return value;
	} else {
		var len = value.length;
		if(len==11 && value.lastIndexOf(" ")==-1){
			value = value.substr(0, 3) + " " + value.substr(3, 4) + " " + value.substr(7, 4);
		}else
		{
			if ((len == 3 || len == 8)) {
				value = value + " ";
			}else if((len == 4 || len == 9)){
				var sz = value.toString().substr(len-1,1);
				if(sz!=" "){
					value = value.substr(0,len-1) + " " + sz;
				}
			}
		}
		return value;
	}
}

/**  
 * @description 手机格式化
 * @param {String} value
 * @return {String}  
 */ 
const idNumberFmt = (value)  => {
	if (isEmpty(value)) {
		return value;
	} else {
		var len = value.length;
		if(len==18 && value.lastIndexOf(" ")==-1){
			value = value.substr(0, 6) + " " + value.substr(6, 4) + " " + value.substr(10, 4) + " " + value.substr(14, 4);
		}else
		{
			if ((len == 3 || len == 8)) {
				value = value + " ";
			}else if((len == 4 || len == 9)){
				var sz = value.toString().substr(len-1,1);
				if(sz!=" "){
					value = value.substr(0,len-1) + " " + sz;
				}
			}
		}
		return value;
	}
}

/**  
 * @description 时间格式化
 * @param {String} value 时间(例如:2019-01-01 11:11:11)
 * @param {String} value 格式字符串(例如:yyyy-MM-dd)
 * @return {String}  
 */  
const timeFmt = (value, format) => {
    let date = new Date(value);
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDate();
    let h = date.getHours();
    let min = date.getMinutes();
    let s = date.getSeconds();
    let result = "";
    if (format == undefined) {
        result = `${y}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d} ${
        h < 10 ? "0" + h : h
      }:${min < 10 ? "0" + min : min}:${s < 10 ? "0" + s : s}`;
    }
    if (format == "yyyy-mm-dd") {
        result = `${y}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d}`;
    }
    if (format == "yyyy-mm") {
        result = `${y}-${m < 10 ? "0" + m : m}`;
    }
    if (format == "mm-dd") {
        result = ` ${mm < 10 ? "0" + mm : mm}:${ddmin < 10 ? "0" + dd : dd}`;
    }
    if (format == "hh:mm") {
        result = ` ${h < 10 ? "0" + h : h}:${min < 10 ? "0" + min : min}`;
    }
    if (format == "yyyy") {
        result = `${y}`;
    }
    return result;
};
 
 //@param {String} method = [get|post] 可选值域包括get和post，get是直接请求，post是提交数据 
 /**  
  * @description 星期格式化
  * @param {String} 例如:2019-05-29 
  */  
const weekFmt = (value) => {
	var curDate = new XDate(new XDate().toString("yyyy-MM-dd"));
	var date = new XDate(vl);
	var days = curDate.diffDays(date);
	switch(days)
	{
		case 0:
			return "今天";
			break;
		case 1:
			return "明天";
			break;
		case 2:
			return "后天";
			break;
		default:
			return "周" + "日一二三四五六".charAt(date.getDay());
			break;
	}
};



/**  
 * @description 富文本内容转换
 * @param {String}  
 */  
const contentFmt = (value) => {
    // #ifdef MP-WEIXIN
    if(value){
    	   value = value.replace(/\<img/gi, '<img style="max-width:100%;height:auto;" ');
    	   value = replaceAll(value,"desctit title","desctit");
		   value = replaceAll(value,'width:677px','100%');
		   value = replaceAll(value,'677','100%');
    }
    // #endif
   return value;
}

module.exports = {
    isEmpty,  
	replaceAll,
	mobileFmt, 
    timeFmt,   
	weekFmt,   
	contentFmt,
	idNumberFmt
}